package b2.widget.friend
{
	import b2.config.*;
	import b2.net.*;
	import b2.util.*;
	import b2.player.*;
	import b2.widget.BasicButton;
	import b2.widget.WidgetManager;
	import b2.widget.sendMail.SendMailTab;
	import b2.widget.tab.*;
	import b2.widget.warehouse.Warehouse;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.external.*;
	import flash.filters.*;
	import flash.text.*;

	public class FriendPane extends Sprite
	{
		private static var instance:FriendPane;
		private var friendTab:Tab;
		
		public function FriendPane()
		{
			super();
			//背景框
			var FriendPaneBgr:Class = ResourceHelper.getInstance().getClassByName("FriendPaneBgr");
			var friendPaneBgr:Sprite = new FriendPaneBgr();
			addChild(friendPaneBgr);
			//好友列表
			friendTab = new Tab("", 1, 10, 2);
			friendTab.x = 14;
			friendTab.y = 60;
			friendTab.setPageSelectorPos(30, 295);
			addChild(friendTab);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "好友";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 13;
			addChild(titleText);
			//邀请按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var buttonInvite:BasicButton = new BasicButton(ButtonBgr, "邀请好友");
			buttonInvite.addEventListener(MouseEvent.CLICK, onButtonInviteClick);
			addChild(buttonInvite);
			if (PlayerInfoManager.getInstance().me.isAdmin || Profile.DEBUG != 0 )
			{
				var buttonGift:BasicButton = new BasicButton(ButtonBgr, "赠送礼物");
				buttonGift.addEventListener(MouseEvent.CLICK, onButtonGiftClick);
				addChild(buttonGift);
				buttonInvite.x = (this.width  - buttonInvite.width) / 2 - 50;
				buttonInvite.y = this.height - buttonInvite.height - 15;
				buttonGift.x = (this.width  - buttonGift.width) / 2 + 50;
				buttonGift.y = this.height - buttonGift.height - 15;
			}
			else
			{
				buttonInvite.x = (this.width  - buttonInvite.width) / 2;
				buttonInvite.y = this.height - buttonInvite.height - 15;
			}
			//关闭按钮
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = this.width - 42;
			closeButton.y = 14;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
		}
		
		public function onButtonInviteClick(event:MouseEvent):void
		{
			ExternalInterface.call("sendRequest", true);
		}
		
		public function onButtonGiftClick(event:MouseEvent):void
		{
			var sendMailTab:SendMailTab = SendMailTab.getInstance();
			sendMailTab.x = 40;
			sendMailTab.y = 100;
			var warehouse:Warehouse = Warehouse.getInstance();
			warehouse.refresh();
			warehouse.x = 386;
			warehouse.y = 100; 
			WidgetManager.getInstance().popOut([warehouse, sendMailTab], true);
		}
		
		public static function getInstance():FriendPane
		{
			if (!instance)
				instance = new FriendPane();
			return instance;
		}
		
		//刷新
		public function refresh():void
		{
			var rrHelper:RRApiHelper = new RRApiHelper();
			rrHelper.getFriends(PlayerInfoManager.getInstance().myRenRenId, onGetFriendsComplete);
		}
		
		private function onGetFriendsComplete(rrPlayerIds:Array, rrPlayerNames:Array):void
		{
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_FRIEND_LIST, onGetFriendList);
			ServerHelper.getInstance().getFriendList(rrPlayerIds);
		}
		
		private function onGetFriendList(event:ServerEvent):void
		{
			var friendIds:Array = event.userData as Array;
			friendIds.sort(sortFriends);
			friendTab.removeAll();
			for (var i:int = 0; i < Math.min(friendIds.length, 30); i++)
			{
				var friendItem:FriendItem = new FriendItem(friendIds[i]);
				friendTab.addItem(friendItem, friendItem.width, friendItem.height);
			}
		}
		
		//好友排序
		private function sortFriends(a:String, b:String):int
		{
			var playerInfoA:PlayerInfo = PlayerInfoManager.getInstance().players[a];
			var playerInfoB:PlayerInfo = PlayerInfoManager.getInstance().players[b];
			if (playerInfoA.level > playerInfoB.level)
			{
				return -1;
			}
			else if (playerInfoA.level < playerInfoB.level)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		
		//关闭
		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
		}

		public function close():void
        {
        	if (this.parent != null)
        	{
        		this.parent.removeChild(this);
        	}
        }
        
	}
}